import mysql.connector


def get_table_structure_sql(host, user, password, database, table):
    # 连接到MySQL数据库
    conn = mysql.connector.connect(
        host=host,
        user=user,
        password=password,
        database=database
    )
    cursor = conn.cursor()

    # 查询表结构的SQL语句
    query = f"SHOW CREATE TABLE {table}"

    # 执行查询
    cursor.execute(query)

    # 获取查询结果
    result = cursor.fetchone()

    # 提取表结构的SQL语句
    table_sql = result[1]

    # 关闭数据库连接
    cursor.close()
    conn.close()

    return table_sql.replace("`", "\"")

def get_table_structure_sql(conn, table):
    # 连接到MySQL数据库
    cursor = conn.cursor()

    # 查询表结构的SQL语句
    query = f"SHOW CREATE TABLE {table}"

    # 执行查询
    cursor.execute(query)

    # 获取查询结果
    result = cursor.fetchone()

    # 提取表结构的SQL语句
    table_sql = result[1]

    # 关闭数据库连接
    cursor.close()
    conn.close()

    return table_sql.replace("`", "\"")

# def get_table_structure_sql
#
# def batchExport(tables):
#     for table in tables:
#         table_structure_sql = get_table_structure_sql(table)




# 设置数据库连接参数
host = 'localhost'  # MySQL主机名
user = 'root'  # MySQL用户名
password = 'egova'  # MySQL密码
database = 'xuanzang'  # 数据库名称
table = 'gis_layer_field_dic'  # 表名

# 获取表结构的SQL语句
conn = mysql.connector.connect(
        host=host,
        user=user,
        password=password,
        database=database
    )
table_sql = get_table_structure_sql(conn, table)
print(table_sql)